﻿using BilibiliUtilities.Live;
using BilibiliUtilities.Live.Lib;
using BilibiliUtilities.Live.Message;
using System.Threading.Tasks;

namespace PnFramework
{
    public class LiveMsg
    {
        /// <summary>
        /// 弹幕消息
        /// </summary>
        public const string DanMu = "DanMuMsg";
        /// <summary>
        /// 人气值消息
        /// </summary>
        public const string Popularity = "PopularityMsg";
        /// <summary>
        /// 礼物连击结束消息
        /// </summary>
        public const string ComboEnd = "ComboEndMsg";
        /// <summary>
        /// 舰长,提督,总督进入房间的消息
        /// </summary>
        public const string EntryEffect = "EntryEffectMsg";
        /// <summary>
        /// 礼物消息
        /// </summary>
        public const string Gift = "GiftMsg";
        /// <summary>
        /// 购买舰长消息
        /// </summary>
        public const string GuardBuy = "GuardBuyMsg";
        /// <summary>
        /// 进入直播间消息
        /// </summary>
        public const string InteractWord = "InteractWordMsg";
        /// <summary>
        /// 直播开始消息
        /// </summary>
        public const string LiveStart = "LiveStartMsg";
        /// <summary>
        /// 直播关闭消息
        /// </summary>
        public const string LiveStop = "LiveStopMsg";
        /// <summary>
        /// 广播消息
        /// </summary>
        public const string Notice = "NoticeMsg";
        /// <summary>
        /// 房间更新消息
        /// </summary>
        public const string RoomUpdate = "RoomUpdateMsg";
        /// <summary>
        /// Vip用户(舰长消息)
        /// </summary>
        public const string UserToast = "UserToastMsg";
        /// <summary>
        /// 欢迎房管进入消息
        /// </summary>
        public const string WelcomeGuard = "WelcomeGuardMsg";
        /// <summary>
        /// 普通用户欢迎消息
        /// </summary>
        public const string WelcomeUser = "WelcomeUserMsg";
    }
    public class LiveMsgMgr : Singleton<LiveMsgMgr>
    {
        private class DanMuMsgHandle : IMessageHandler
        {
            /// <summary>
            /// 处理人气值
            /// </summary>
            /// <param name="audiences">类型是int,为人气值</param>
            Task IMessageHandler.AudiencesHandlerAsync(int audiences)
            {
                EventMgr.Instance.Trigger(LiveMsg.Popularity, audiences);
                return null;
            }
            /// <summary>
            /// 送礼物的连击结束后返回的信息
            /// </summary>
            Task IMessageHandler.ComboEndMessageHandlerAsync(ComboEndMessage message)
            {
                EventMgr.Instance.Trigger(LiveMsg.ComboEnd, message);
                return null;
            }
            /// <summary>
            /// 接收以及处理弹幕消息
            /// </summary>
            /// <param name="danmuMessage">弹幕消息</param>
            Task IMessageHandler.DanmuMessageHandlerAsync(DanmuMessage message)
            {
                EventMgr.Instance.Trigger(LiveMsg.DanMu, message);
                return null;
            }
            /// <summary>
            /// 舰长,提督,总督进入房间的通知
            /// </summary>
            Task IMessageHandler.EntryEffectMessageHandlerAsync(EntryEffectMessage message)
            {
                EventMgr.Instance.Trigger(LiveMsg.EntryEffect, message);
                return null;
            }
            /// <summary>
            /// 礼物消息
            /// </summary>
            Task IMessageHandler.GiftMessageHandlerAsync(GiftMessage message)
            {
                EventMgr.Instance.Trigger(LiveMsg.Gift, message);
                return null;
            }
            /// <summary>
            /// 购买舰长的信息处理
            /// </summary>
            Task IMessageHandler.GuardBuyMessageHandlerAsync(GuardBuyMessage message)
            {
                EventMgr.Instance.Trigger(LiveMsg.GuardBuy, message);
                return null;
            }
            /// <summary>
            /// 进入直播间
            /// </summary>
            Task IMessageHandler.InteractWordMessageHandlerAsync(InteractWordMessage message)
            {
                EventMgr.Instance.Trigger(LiveMsg.InteractWord, message);
                return null;
            }
            /// <summary>
            /// 直播间开启的通知
            /// </summary>
            Task IMessageHandler.LiveStartMessageHandlerAsync(int roomId)
            {
                EventMgr.Instance.Trigger(LiveMsg.LiveStart, roomId);
                return null;
            }
            /// <summary>
            /// 直播间关闭的通知
            /// </summary>
            Task IMessageHandler.LiveStopMessageHandlerAsync(int roomId)
            {
                EventMgr.Instance.Trigger(LiveMsg.LiveStop, roomId);
                return null;
            }
            /// <summary>
            /// 处理大范围通知消息,比如有谁的房间被送了小电视
            /// </summary>
            Task IMessageHandler.NoticeMessageHandlerAsync(NoticeMessage message)
            {
                EventMgr.Instance.Trigger(LiveMsg.Notice, message);
                return null;
            }
            /// <summary>
            /// 房间的更新信息
            /// </summary>
            Task IMessageHandler.RoomUpdateMessageHandlerAsync(RoomUpdateMessage message)
            {
                EventMgr.Instance.Trigger(LiveMsg.RoomUpdate, message);
                return null;
            }
            /// <summary>
            /// 未知 应该也是类似舰长消息
            /// </summary>
            Task IMessageHandler.UserToastMessageHandlerAsync(UserToastMessage message)
            {
                EventMgr.Instance.Trigger(LiveMsg.UserToast, message);
                return null;
            }
            /// <summary>
            /// 欢迎房管进入的消息
            /// </summary>
            Task IMessageHandler.WelcomeGuardMessageHandlerAsync(WelcomeGuardMessage message)
            {
                EventMgr.Instance.Trigger(LiveMsg.WelcomeGuard, message);
                return null;
            }
            /// <summary>
            /// 普通的欢迎信息,比如月费老爷
            /// </summary>
            Task IMessageHandler.WelcomeMessageHandlerAsync(WelcomeMessage message)
            {
                EventMgr.Instance.Trigger(LiveMsg.WelcomeUser, message);
                return null;
            }
        }
        /// <summary>
        /// 链接直播间
        /// </summary>
        /// <param name="roomId">房间号</param>
        public async void ConnectLive(int roomId)
        {
            var handle = new DanMuMsgHandle();
            //创建直播间 传入房间号 和 消息处理器
            var room = new LiveRoom(roomId, handle);
            //返回是否连接成功
            if (await room.ConnectAsync())
            {
                //Debug.Log("连接成功！开始获取弹幕");
                await room.ReadMessageLoop();
            }
        }
    }
}